﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.VCProjectEngine;
using EnvDTE80;

namespace Gauss.Isp
{
    class IspBuildThread
    {
        private DTE2 _dte;
        //private VCConfiguration _vcConfig;
        private string _solConfig;
        private object _buildDone;
        private object _threadDone;
        private Exception _buildFailed;
        private bool _buildStarted;
        private string _projUniqueName;

        public IspBuildThread(DTE2 dte, string solConfig, string projUniqueName)//VCConfiguration vcConfig)
        {
            _dte = dte;
            //_vcConfig = vcConfig;
            _solConfig = solConfig;
            _projUniqueName = projUniqueName;
            _buildDone = new object();
            _threadDone = new object();
        }

        public void Build() 
        {
            EnvDTE._dispBuildEvents_OnBuildProjConfigDoneEventHandler handler = 
                new EnvDTE._dispBuildEvents_OnBuildProjConfigDoneEventHandler(BuildEvents_OnBuildProjConfigDone);
            EnvDTE.BuildEvents buildEvents = _dte.Events.BuildEvents;
            _buildFailed = null;
            _buildStarted = false;

            //try
            //{
                buildEvents.OnBuildProjConfigDone += handler;

                //System.Threading.Thread thread = new System.Threading.Thread(
                //    delegate()
                //    {
                        try
                        {
                            _buildStarted = true;
                            //_vcConfig.Rebuild();
                            _dte.Solution.SolutionBuild.BuildProject(_solConfig, _projUniqueName, false);
                            while (_dte.Solution.SolutionBuild.BuildState == EnvDTE.vsBuildState.vsBuildStateInProgress ||
                                _dte.Solution.SolutionBuild.BuildState == EnvDTE.vsBuildState.vsBuildStateNotStarted) { }
                        }
                        catch (Exception e)
                        {
                            this._buildFailed = e;
                            _buildStarted = false;
                        }
                //    });
                //thread.Start();

                //while (thread.IsAlive)
                //{
                //    thread.Join(int.MaxValue - 1);
                //}

            //    if (_buildStarted)
            //    {
            //        lock (_buildDone)
            //        {
            //            System.Threading.Monitor.Wait(_buildDone);
            //        }
            //    }
            //}
            //catch (System.Threading.ThreadAbortException)
            //{
            //    try
            //    {
            //        _dte.ExecuteCommand("Build.Cancel", "");
            //        lock (_buildDone)
            //        {
            //            System.Threading.Monitor.Wait(_buildDone);
            //        }
            //    }
            //    catch { }

            //    throw;
            //}
            //finally
            //{
                buildEvents.OnBuildProjConfigDone -= handler;
            //}

            if (_buildFailed != null)
            {
                throw _buildFailed;
            }
        }

        void BuildEvents_OnBuildProjConfigDone(string Project, string ProjectConfig, string Platform, string SolutionConfig, bool Success)
        {
            if (!Success)
            {
                _buildFailed = new ApplicationException(Utils.StringFormat(Resources.Message_ProjectBuildFailed, Project));
            }

            lock (_buildDone)
            {
                System.Threading.Monitor.Pulse(_buildDone);
            }
        }
    }
}
